"use strict";
exports.__esModule = true;
exports.getLogStr = exports.getDaysBetween = exports.isNumber = exports.getFlatternDistance = exports.getGreatCircleDistance = exports.formatTime = exports.zeroNum = void 0;
var EARTH_RADIUS = 6378137.0; //单位m
var PI = Math.PI;
function getRad(d) {
    return d * PI / 180.0;
}
function zeroNum(num) {
    return (num + '').length < 2 ? ('0' + num) : num;
}
exports.zeroNum = zeroNum;
function formatTime(time) {
    try {
        var date = new Date(time);
        return date.getFullYear() + '-' + zeroNum(Number(date.getMonth() + 1)) + '-' + zeroNum(date.getDate()) + ' ' + zeroNum(date.getHours()) + ':' + zeroNum(date.getMinutes()) + ':' + zeroNum(date.getSeconds());
    }
    catch (e) {
        return '';
    }
}
exports.formatTime = formatTime;
function getGreatCircleDistance(lat1, lng1, lat2, lng2) {
    var radLat1 = getRad(lat1);
    var radLat2 = getRad(lat2);
    var a = radLat1 - radLat2;
    var b = getRad(lng1) - getRad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000.0;
    return s;
}
exports.getGreatCircleDistance = getGreatCircleDistance;
/** 计算两经纬度之间的距离，单位是m
 * approx distance between two points on earth ellipsoid
 */
function getFlatternDistance(lat1, lng1, lat2, lng2) {
    var radLat1 = lat1 * Math.PI / 180.0;
    var radLat2 = lat2 * Math.PI / 180.0;
    var a = radLat1 - radLat2;
    var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    return s * 1000;
    /*
    const PI = Math.PI
    const EARTH_RADIUS = 6378137.0
    function getRad (d) {
        return d * PI / 180.0
    }
    let f = getRad((lat1 + lat2) / 2)
    let g = getRad((lat1 - lat2) / 2)
    let l = getRad((lng1 - lng2) / 2)
    let sg = Math.sin(g)
    let sl = Math.sin(l)
    let sf = Math.sin(f)

    let s, c, w, r, d, h1, h2
    let a = EARTH_RADIUS
    let fl = 1 / 298.257

    sg = sg * sg
    sl = sl * sl
    sf = sf * sf

    s = sg * (1 - sl) + (1 - sf) * sl
    c = (1 - sg) * (1 - sl) + sf * sl

    w = Math.atan(Math.sqrt(s / c))
    r = Math.sqrt(s * c) / w
    d = 2 * w * a
    h1 = (3 * r - 1) / 2 / c
    h2 = (3 * r + 1) / 2 / s

    return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg))
    */
}
exports.getFlatternDistance = getFlatternDistance;
// let result1 = getGreatCircleDistance(30.1234, 140.1234, 30.3456, 140.3456)
// let result2 = getFlatternDistance(30.1234, 140.1234, 30.3456, 140.3456)
// console.log(result1) // 32688.3298
// console.log(result2) // 32622.43244078783
function isNumber(num) {
    return /^\d+$/.test("".concat(num));
}
exports.isNumber = isNumber;
function getDaysBetween(startTime, endTime) {
    var sDate = new Date(startTime).getTime();
    var eDate = new Date(endTime).getTime();
    if (sDate > eDate) {
        return 0;
    }
    if (sDate === eDate) {
        return 1;
    }
    return (eDate - sDate) / (24 * 60 * 60 * 1000) + 1;
}
exports.getDaysBetween = getDaysBetween;
function getLogStr(str) {
    return "/********************************************************\n    ".concat(str, "\n    ********************************************************/\n");
}
exports.getLogStr = getLogStr;
